from fsevents import Observer, Stream, IN_CREATE, IN_MOVED_TO, IN_MODIFY
# NOTE: require http://pypi.python.org/pypi/MacFSEvents/
# XXX Cause error when sub-directory removed.

def mainloop(path, pattern, callback, recursive=True):
    def fschanged(event):
        if pattern.search(event.name) and \
                event.mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY):
            callback()

    callback()
    observer = Observer()
    stream = Stream(fschanged, path, file_events=True)
    observer.schedule(stream)
    observer.run()
